#include <bits/stdc++.h>
// 2025/03/13
// tag: 
// Author: Forever
using namespace std;

using int64 = int64_t;

int g[501][501], t, n;

signed main()
{
    ios::sync_with_stdio(false);
    cin.tie(0);
    cout.tie(0);
    cin >> t;
    while (t--){
        cin >> n;
        for (int i = 1; i <= n; i++)
            for (int j = 1; j <= n; j++)
                cin >> g[i][j];
        int ans = 0;
        for (int i = 1; i <= n; i++){
            int minn = 0;
            for (int j = 0; j <= n - i; j++)
                minn = min(minn, g[i + j][j + 1]);
            ans -= minn;
            if (i != 1){
                minn = 0;
                for (int j = 0; j <= n - i; j++)
                    minn = min(minn, g[j + 1][i + j]);
                ans -= minn;
            }
        }
        cout << ans << endl;
    }
    return 0;
}